package com.example.a022.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import idea.analyzesystem.scrollview.IdeaScrollView;
import idea.analyzesystem.scrollview.IdeaViewPager;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by 022 on 2018/9/3.
 */

public class PorductDetailsActivity extends AppCompatActivity {
    private IdeaViewPager viewPager;
    private IdeaScrollView ideaScrollView;
    private TextView text;
    private LinearLayout header;
    private RadioGroup radioGroup;
    private LinearLayout headerParent;
    private NonScrollGridView gridView,gridView2;
    private ListView listView;
    private ImageView icon;
    private View layer;
    private float currentPercentage = 0;
    private RadioGroup.OnCheckedChangeListener radioGroupListener =new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            for(int i=0;i<radioGroup.getChildCount();i++){
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                radioButton.setTextColor(radioButton.isChecked()?getRadioCheckedAlphaColor(currentPercentage):getRadioAlphaColor(currentPercentage));
                if(radioButton.isChecked()&&isNeedScrollTo){
                    ideaScrollView.setPosition(i);
                }
            }
        }
    };
    private boolean isNeedScrollTo = true;

    private List<String> data;
    private List<String> data2;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_details);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        StatusBarCompat.translucentStatusBar(this);
        header = (LinearLayout)findViewById(R.id.header);
        headerParent = (LinearLayout)findViewById(R.id.headerParent);
        icon = (ImageView)findViewById(R.id.icon);
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        ideaScrollView = (IdeaScrollView)findViewById(R.id.ideaScrollView);
        viewPager = (IdeaViewPager)findViewById(R.id.viewPager);
        layer = findViewById(R.id.layer);

        gridView = (NonScrollGridView) findViewById(R.id.gridView);
        gridView2 = (NonScrollGridView) findViewById(R.id.gridView2);

        Rect rectangle= new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
        ideaScrollView.setViewPager(viewPager,getMeasureHeight(headerParent)-rectangle.top);
        icon.setImageAlpha(0);
        radioGroup.setAlpha(0);
        radioGroup.check(radioGroup.getChildAt(0).getId());

        data = new ArrayList<>();
        data.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");

        gridView2.setAdapter(new CommodityDetailAdapter(PorductDetailsActivity.this,data));

        data2 = new ArrayList<>();
        data2.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data2.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data2.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data2.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data2.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data2.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");
        data2.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data2.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data2.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data2.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data2.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data2.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");

        gridView.setAdapter(new FragmentAdapter(PorductDetailsActivity.this,data2));

        View one = findViewById(R.id.one);
      //  View two = findViewById(R.id.two);
        View four = findViewById(R.id.four);
        View three = findViewById(R.id.three);
        ArrayList<Integer> araryDistance = new ArrayList<>();

        Log.d("ddd","Height"+gridView2.getMeasuredHeight());
        ListAdapter listAdapter = gridView2.getAdapter();
        int totalHeight = 0;
        Log.d("ddd","listAdapter.getCount()"+listAdapter.getCount());
        for (int i = 0;i<listAdapter.getCount();i++){
            int sd = listAdapter.getView(i,null,gridView2).getMeasuredHeight();
            Log.d("ddd","sd"+sd);

            View listItem = listAdapter.getView(i,null,gridView2);
            int desiredWidth = View.MeasureSpec.makeMeasureSpec(gridView2.getHeight(),
                    View.MeasureSpec.EXACTLY);
            listItem.measure(0,desiredWidth);
            totalHeight += listItem.getMeasuredHeight();
            Log.d("ddd","totalHeight"+totalHeight);
    }

        Log.d("ddd","getMeasureHeight(one)"+getMeasureHeight(one));
        Log.d("ddd","getMeasureHeight(three)"+getMeasureHeight(three));
        Log.d("ddd","getMeasureHeight(headerParent)"+getMeasureHeight(headerParent));
        Log.d("ddd","height"+gridView.getHeight());
        Log.d("ddd","height"+gridView2.getHeight());



        WindowManager wm = (WindowManager) PorductDetailsActivity.this.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        Log.d("ttt",three.getTop()+"");
        Log.d("ttt",four.getTop()+"");

        araryDistance.add(0);
        araryDistance.add(three.getTop());
        araryDistance.add(four.getTop());

//        araryDistance.add(pxTodip(PorductDetailsActivity.this,getMeasureHeight(one))-pxTodip(PorductDetailsActivity.this,getMeasureHeight(headerParent)));
//        araryDistance.add(pxTodip(PorductDetailsActivity.this,getMeasureHeight(one))+pxTodip(PorductDetailsActivity.this,getMeasureHeight(three))-pxTodip(PorductDetailsActivity.this,getMeasureHeight(headerParent)));


        ideaScrollView.setArrayDistance(araryDistance);

        ideaScrollView.setOnScrollChangedColorListener(new IdeaScrollView.OnScrollChangedColorListener() {
            @Override
            public void onChanged(float percentage) {

                int color = getAlphaColor(percentage>0.9f?1.0f:percentage);
                header.setBackgroundDrawable(new ColorDrawable(color));
                radioGroup.setBackgroundDrawable(new ColorDrawable(color));
                icon.setImageAlpha((int) ((percentage>0.9f?1.0f:percentage)*255));
                radioGroup.setAlpha((percentage>0.9f?1.0f:percentage)*255);

                setRadioButtonTextColor(percentage);

            }

            @Override
            public void onChangedFirstColor(float percentage) {

            }

            @Override
            public void onChangedSecondColor(float percentage) {

            }
        });

        ideaScrollView.setOnSelectedIndicateChangedListener(new IdeaScrollView.OnSelectedIndicateChangedListener() {
            @Override
            public void onSelectedChanged(int position) {
                isNeedScrollTo = false;
                radioGroup.check(radioGroup.getChildAt(position).getId());
                isNeedScrollTo = true;
            }
        });

        radioGroup.setOnCheckedChangeListener(radioGroupListener);
    }

    public static int pxTodip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public void setRadioButtonTextColor(float percentage){
        if(Math.abs(percentage-currentPercentage)>=0.1f){
            for(int i=0;i<radioGroup.getChildCount();i++){
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                radioButton.setTextColor(radioButton.isChecked()?getRadioCheckedAlphaColor(percentage):getRadioAlphaColor(percentage));
            }
            this.currentPercentage = percentage;
        }
    }

    public int getMeasureHeight(View view){
        int width = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        return view.getMeasuredHeight();
    }


    public int getAlphaColor(float f){
        return Color.argb((int) (f*255),0x09,0xc1,0xf4);
    }

    public int getLayerAlphaColor(float f){
        return Color.argb((int) (f*255),0x09,0xc1,0xf4);
    }

    public int getRadioCheckedAlphaColor(float f){
        return Color.argb((int) (f*255),0x44,0x44,0x44);
    }

    public int getRadioAlphaColor(float f){
        return Color.argb((int) (f*255),0xFF,0xFF,0xFF);
    }


}
